﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Collections;

using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Text.RegularExpressions;
using System.Web;

using System.Xml.Xsl;
using System.Xml.Schema;

using System.Diagnostics;
using System.Reflection;


namespace SchematronTestFramework
{
    class Reporter
    {

        public string reportpath;
        public Reporter(string rep)
        {
            reportpath = rep;
        }

        private static object privateObjectLock = new object();
        public void CreateMessage(string fileName, int line, string code, string description)
        {
            lock (privateObjectLock)
            {
                FileStream fs = new FileStream(reportpath, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write("<message><file>\n");
                sw.Write(fileName + "\n");
                sw.Write("</file> <line>\n");

                sw.Write(line + "\n");
                sw.Write("</line> <type>\n");
                sw.Write(code);
                sw.Write("</type> <code>\n");
                sw.Write(code + "\n");
                sw.Write("</code> <desc>\n");

                sw.Write(description + "\n");
                sw.Write("</desc></message>\n");

                sw.Flush();
                sw.Close();
                fs.Close();
            }
        }

        public void GenerateXmlReport()
        {


            FileStream fs = new FileStream(reportpath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            string dic = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            sw.Write("<?xml version=\"1.0\" ?>\n");
            sw.Write("<?xml-stylesheet type=\"text/xsl\" encoding=\"UTF-8\" href=\"stf.xsl\" version=\"1.0\"?>\n");
            sw.Write("<doc>\n");


            sw.Flush();
            sw.Close();
            fs.Close();



        }
        public void EndReport()
        {

            FileStream fs = new FileStream(reportpath, FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write("</doc>\n");


            sw.Flush();
            sw.Close();
            fs.Close();

            string xsl = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "/stf.xsl";
            FileInfo xslinfo = new FileInfo(xsl);
            if (xslinfo.Exists)
            {
            }
            else
            {
                File.Copy(xsl, System.IO.Path.GetDirectoryName(reportpath) + "/stf.xsl");
            }

        }

    }


}
